﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Modeling_Teacher_ListStudent : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["WelcomeUser"] != null)
        {
            if (Session["EmployeeID"] != null && Session["Role"].ToString().Equals("EAS"))
            {
                UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

                if (udm.createConnection() != null)
                {
                    gvAdmission.DataSource = udm.showAdmission(udm.createConnection());
                    if (!Page.IsPostBack)
                    {
                        gvAdmission.DataBind();
                    }
                }
                else
                {
                    Response.Redirect("~/ErrorPage.aspx");
                }
            }
            else
            {
                Response.Redirect("~/LoginMessage.aspx?id=1");
            }
        }
        else
        {
            Response.Redirect("~/Login.aspx?id=2");
        }
    }
    protected void gvAdmission_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvAdmission.EditIndex = e.NewEditIndex;
        gvAdmission.DataBind();

        DropDownList ddl = (DropDownList)gvAdmission.Rows[e.NewEditIndex].Cells[1].Controls[1];

        ddl.Items.Add(new ListItem("Checking","0"));
        ddl.Items.Add(new ListItem("Accept", "1"));
        ddl.Items.Add(new ListItem("Reject", "2"));
    }
    protected void gvAdmission_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvAdmission.EditIndex = -1;
        gvAdmission.DataBind();
    }
    protected void gvAdmission_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = Convert.ToInt32(gvAdmission.Rows[e.RowIndex].Cells[3].Text.ToString());

        DropDownList ddl = (DropDownList)gvAdmission.Rows[e.RowIndex].Cells[1].Controls[1];

        int status = Convert.ToInt32(ddl.SelectedValue.ToString());

        UniversityDatabaseManagerment udm = new UniversityDatabaseManagerment();

        if (udm.createConnection() != null)
        {
            string firstName = gvAdmission.Rows[e.RowIndex].Cells[4].Text;
            string lastName = gvAdmission.Rows[e.RowIndex].Cells[5].Text;
            DateTime dateOfBirth = Convert.ToDateTime(gvAdmission.Rows[e.RowIndex].Cells[7].Text);
            string street = gvAdmission.Rows[e.RowIndex].Cells[8].Text;
            string state = gvAdmission.Rows[e.RowIndex].Cells[9].Text;
            string city = gvAdmission.Rows[e.RowIndex].Cells[10].Text;
            string phone = gvAdmission.Rows[e.RowIndex].Cells[11].Text;
            string email = gvAdmission.Rows[e.RowIndex].Cells[12].Text;
            int gender = 0;

            if (gvAdmission.Rows[e.RowIndex].Cells[6].Text.Equals("Female"))
            {
                gender = 1;
            }

            Image img = (Image)gvAdmission.Rows[e.RowIndex].Cells[2].Controls[0];

            string filePath = img.ImageUrl;

            GenerateDates gd = new GenerateDates();

            string nextID = gd.generateStuID(udm.showStudentsForMaintainIDDESC(udm.createConnection()).Tables[0].Rows[0][0].ToString());

            string username = firstName + "_" + nextID;

            string pass = gd.EncodePassword("123456");

            if (udm.updateAdmission(udm.createConnection(), id, status))
            {
                if (status == 1)
                {
                    if (!udm.addStudent(udm.createConnection(), nextID, firstName, lastName, username, pass,gender, dateOfBirth, street, state, city, phone, email, filePath))
                    {
                        Response.Redirect("~/ErrorPage.aspx");
                    }
                }

                gvAdmission.EditIndex = -1;
                gvAdmission.DataSource = udm.showAdmission(udm.createConnection());
                gvAdmission.DataBind();
            }
            else
            {
                Response.Redirect("~/ErrorPage.aspx");
            }
            
        }
        else
        {
            Response.Redirect("~/ErrorPage.aspx");
        }
    }
}